﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using ConsoleApp.Dapper;
using Dapper;
using IBatisNet.DataMapper;

namespace WebApp.Repositories
{
    public interface ICategoryRepository
    {
        IList<Day> GetDays();

        void Insert(Day day);
    }

    public class DBCategoryRepository : ICategoryRepository
    {
        private IDbConnection _dbconn = null;

        public DBCategoryRepository(IDbConnection dbconn)
        {
            _dbconn = dbconn;
        }

        public IList<Day> GetDays()
        {
            return _dbconn.Query<Day>("select * from day").ToList();
        }

        public void Insert(Day day)
        {
            string sql = string.Format(@"INSERT INTO day (AutoId,DayName) VALUES({0},""{1}"");SELECT @@identity;", day.AutoId, day.DayName);
            int i = _dbconn.Query<int>(sql).FirstOrDefault();
            day.AutoId = i;
        }
    }


    public class MyBatisCategoryRepository : ICategoryRepository
    {
        private ISqlMapper _sqlMapper = null;

        public MyBatisCategoryRepository(ISqlMapper sqlMapper)
        {
            _sqlMapper = sqlMapper;
        }

        public IList<Day> GetDays()
        {
            return _sqlMapper.QueryForList<Day>("SelectAllDay", null);
        }

        public void Insert(Day day)
        {
            _sqlMapper.Insert("InsertDay", day);
        }
    }
}